package tuya;

/**
 * @author xizizzz
 * @description: TODO
 * @date 2021-9-14下午 08:16
 */
public class Test2 {

    public class ListNode {
        int val;
        ListNode next = null;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public static ListNode removeNthFromEnd(ListNode head, int n) {
        //如果链表本身为空或者只有一个结点，直接返回空即可
        if (head == null || head.next == null) {
            return null;
        }
        ListNode cur = head;
        ListNode pre = head;
        for (int i = 0; i < n; i++) {
            cur = cur.next;
        }
        if (cur == null) {
            head = head.next;
            return head;
        }
        while (cur.next != null) {
            cur = cur.next;
            pre = pre.next;
        }

        pre.next = pre.next.next;
        return head;
    }

}
